package com.imike.akka;

import scala.concurrent.Await;
import scala.concurrent.Future;
import scala.concurrent.duration.Duration;
import akka.actor.ActorRef;
import akka.actor.ActorSystem;
import akka.actor.Props;
import akka.pattern.Patterns;
import akka.util.Timeout;

public class ActorTest {

    
    @SuppressWarnings({ "unchecked", "rawtypes" })
    public static void main(String[] args) throws Exception {
        
        final ActorSystem system = ActorSystem.create("MySystem");
        final ActorRef myActor = system.actorOf(Props.create(StudentActor.class),"StudentActor");
        
        Timeout timeout = new Timeout(Duration.create(5, "seconds"));
        Future<Object> future = Patterns.ask(myActor, "get", timeout);
        
        //异步调用
        future.onSuccess(new PrintStudentInfo(), system.dispatcher());
        System.out.println("after invoke");
        Thread.sleep(2000);
        
        //同步调用
//        Student s = null;
//        try {
//            s = (Student) Await.result(future, timeout.duration());
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        
//        System.out.println("name:" + s.getName() + ",id:" + s.getId() + ",gender:" + s.getGender() + ",age:" + s.getAge());
        
        system.shutdown();
    }

}
